package com.black.common.web;

import java.util.HashMap;
import java.util.List;

/**
 * 通用返回数据对象
 */
public class ApiResult extends HashMap<String, Object> {

    /**
     *
     */
    private static final long serialVersionUID = 3353972966306119801L;

    public ApiResult setCode(Object value) {
        this.put("CODE", value);
        return this;
    }

    public ApiResult setName(Object value) {
        this.put("NAME", value);
        return this;
    }

    public ApiResult add(String key, Object value) {
        this.put(key, value);
        return this;
    }

    public ApiResult add(Object value) {
        if (value == null) {
            return this;
        }
        String key = value.getClass().getSimpleName();
        this.put(key.substring(0, 1).toLowerCase() + key.substring(1), value);
        return this;
    }

    @SuppressWarnings("rawtypes")
    public ApiResult addList(List values, Class<?> t) {
        if (values == null) {
            return this;
        }
        String key = t.getSimpleName() + "s";
        this.put(key.substring(0, 1).toLowerCase() + key.substring(1), values);
        return this;
    }

    @SuppressWarnings("unchecked")
    public <T> T get(Class<T> t) {
        String key = t.getSimpleName();
        key = key.substring(0, 1).toLowerCase() + key.substring(1);
        return (T) this.get(key);
    }

    @SuppressWarnings("unchecked")
    public <T> T getList(Class<T> t) {
        String key = t.getSimpleName();
        key = key.substring(0, 1).toLowerCase() + key.substring(1) + "s";
        return (T) this.get(key);
    }

    @SuppressWarnings("unchecked")
    public <T> T get(String key, Class<T> t) {
        return (T) this.get(key);
    }

    public static ApiResult newInstance() {
        return new ApiResult();
    }

}